<?php
use yii\helpers\Url;
use common\models\goods\GoodsAttrVal;
use common\models\product\Product;

$attrs=$goodsType->attrs;
?>

<div class="sku-list">
<?php
    $skus=[];
    foreach ($attrs['sku'] as $attr):
    ?>
    <div class="sku-item">
        <div class="required"><?=$attr->name?></div>
        <ul attr_id="<?=$attr->id?>" is_aliases="<?=$attr->is_aliases?>" is_upload_img="<?=$attr->is_upload_img?>">
            <?php 
            $i=0;
            $list=[];
            foreach ($attr->vals as $val):
            ?>
            <li>
                <label>
                    <span id="sku_id_<?=$attr->id?>_<?=$i?>"><input name="GoodsForm[sku][<?=$attr->id?>][<?=$i?>][id]" type="hidden" value="<?php if(isset($goodsForm->sku[$attr->id][$val])){echo $goodsForm->sku[$attr->id][$val]->id;}?>"></span>
                    <input name="GoodsForm[sku][<?=$attr->id?>][<?=$i?>][val]" value="<?=$val?>" type="checkbox" id="sku_<?=$attr->id?>_<?=$i?>" attr_id="<?=$attr->id?>" index="<?=$i?>" is_aliases="<?=$attr->is_aliases?>" is_upload_img="<?=$attr->is_upload_img?>" class="sku-checkbox" <?php if(isset($goodsForm->sku[$attr->id][$val])){echo 'checked=""';}?>>
                    <?php 
                    $name=$val;
                    if($attr->is_color):
                        $arr = explode('|', $val);
                        $type = substr($arr[1], 0,1)=='#'?1:2;
                        $name=$arr[0];
                        ?>
                        <div class="sku-color <?php if($type==2){echo $arr[1];}?>" style="<?php if($type==1){echo 'background:'.$arr[1].';';}?>"></div>
                    <?php endif;?>
                    <span class="sku-item-name <?php if($attr->is_aliases && isset($goodsForm->sku[$attr->id][$val])){echo 'none';}?>" id="sku_name_<?=$attr->id?>_<?=$i?>"><?=$name?></span>
                </label>
                <?php
                $customName=$name;
                if($attr->is_aliases && isset($goodsForm->sku[$attr->id][$val])){
                    $customName=$goodsForm->sku[$attr->id][$val]->aliases;
                }
                ?>
                <span id="sku_aliases_<?=$attr->id?>_<?=$i?>">
                    <input type="text" name="GoodsForm[sku][<?=$attr->id?>][<?=$i?>][aliases]" id="sku_name_custom_<?=$attr->id?>_<?=$i?>" attr_id="<?=$attr->id?>" index="<?=$i?>" class="sku-custom-name <?php if(!($attr->is_aliases && isset($goodsForm->sku[$attr->id][$val]))){echo 'none';}?>" value="<?=$customName?>" />
                </span>
            </li>
            <?php 
            $list[]=[$attr->id,$val,$i,$customName];
            $i++;
            endforeach;?>
        </ul>
        <div class="clear"></div>
    </div>
    <?php 
    $skus[]=$list;
    endforeach;?>
    <div class="clear"></div>
</div>

<!--上传规格图片-->
<div class="sku-img">
    <?php foreach ($attrs['sku'] as $attr):?>
        <?php if($attr->is_upload_img):?>
            <div class="upload-img-list"  attr_id="<?=$attr->id?>">
                <table cellpadding="1" cellspacing="1" class="sku-img-list">
                    <thead>
                        <th><?=$attr->name?></th>
                        <th>图片</th>
                    </thead>
                    <?php 
                    $i=0;
                    foreach ($attr['vals'] as $val):
                    ?>
                    <tr <?php if(!isset($goodsForm->sku[$attr->id][$val])){echo 'class="none"';}?>>
                        <td>
                            <?php 
                            $name=$val;
                            if($attr->is_color):
                                $arr = explode('|', $val);
                                $type = substr($arr[1], 0,1)=='#'?1:2;
                                $name=$arr[0];
                                ?>
                            <div class="sku-color <?php if($type==2){echo $arr[1];}?>" style="<?php if($type==1){echo 'background:'.$arr[1].';';}?>"></div>
                            <?php endif;?>
                            <span class="sku-item-name" sku_name_id="<?=$attr->id?>_<?=$i?>">
                                <?php
                                if($attr->is_aliases && isset($goodsForm->sku[$attr->id][$val])){
                                    echo $goodsForm->sku[$attr->id][$val]->aliases;
                                }else{
                                    echo $name;
                                }?>
                            </span>
                        </td>
                        <td>
                            <input type="button" value="上传" class="img-select" onclick="uploadAttrImg('<?=$attr->id?>_<?=$i?>');" >
                            <span id="sku_img_<?=$attr->id?>_<?=$i?>">
                                <input type="hidden" value="<?php if(isset($goodsForm->sku[$attr->id][$val]->img_url)){echo $goodsForm->sku[$attr->id][$val]->img_url;}?>" name="GoodsForm[sku][<?=$attr->id?>][<?=$i?>][img]" id="attr_img_<?=$attr->id?>_<?=$i?>" />
                            </span>
                            <span class="thumb">
                                <img id="attr_img_src_<?=$attr->id?>_<?=$i?>" src="<?php if(isset($goodsForm->sku[$attr->id][$val]->img_url) && !empty($goodsForm->sku[$attr->id][$val]->img_url)){echo Yii::$app->params['urls']['img'].$goodsForm->sku[$attr->id][$val]->img_url;}?>">
                            </span>
                            <a href="javascript:setAttrImgUrl('<?=$attr->id?>_<?=$i?>','','');void(0);">删除</a>
                        </td>
                    </tr>
                    <?php 
                    $i++;
                    endforeach;?>
                </table>
            </div>
        <?php endif;?>
    <?php endforeach;?>
</div>

<?php foreach ($attrs['spec'] as $attr):?>

<?php endforeach;?>


<!--货品列表-->
<div id="product_map">
    <table cellpadding="1" cellspacing="1">
        <tr class="title">
            <?php foreach ($attrs['sku'] as $attr):?>
            <th><?=$attr->name?></th>
            <?php endforeach;?>
            <th class="required">货品编号</th>
            <th>国际码</th>
            <th>厂家编号</th>
            <th>短名称(打印条码用)</th>
            <th class="required">成本价</th>
            <th>市场价</th>
            <th>货位</th>
            <th class="required">重量(克)</th>
            <th>零售价</th>
            <th>混批价</th>
            <th>整箱价</th>
        </tr>

        <?php
        //提取所有货品信息,起订量，价格梯度
        foreach ($goodsForm->products as $product) {
            $products[$product->goods_attr]=$product;
        }

        //提取所有已选择的规格
        $AttrVals=[];
        $goodsAttrVals = GoodsAttrVal::findAll(['goods_id'=>$goodsForm->id]);
        foreach ($goodsAttrVals as $val) {
            $AttrVals[$val->attr_id][$val->attr_value]=$val->id;
        }
        
        //生成已选择的规格的组合
        $allSku=combos($skus);
        
        $i=0;
        foreach ($allSku as $items):
            $id='pro_item_';
            $con=$con2='';
            $attrIds='';
            
            foreach ($items as $item){
                $id.=$con.$item[0].'__'.$item[2];
                $con='_';
                if(isset($AttrVals[$item[0]][$item[1]])){
                    $attrIds.=$con2.$AttrVals[$item[0]][$item[1]];
                    $con2=',';
                }
            }

            $cur = $attrIds!='' && isset($products[$attrIds]) ? $products[$attrIds] : new Product;
            $mins=$cur->mins;
            $prices=$cur->prices;
            
            ?>

        <tr id="<?=$id?>" class="product-map-item <?php if(!intval($cur->id)>0){echo 'none';}?>">
            <?php foreach ($items as $item):?>
                <td>
                    <span class="sku-item-name" sku_name_id="<?=$item[0]?>_<?=$item[2]?>">
                        <?=$item[3]?>
                    </span>
                </td>
            <?php endforeach;?>
            <td>
                <input type="hidden" name="GoodsForm[products][<?=$i?>][id]" id="product_id_<?=$i?>" value="<?=$cur->id?>" />
                <input type="hidden" name="GoodsForm[products][<?=$i?>][goods_attr]" value="<?=$cur->goods_attr?>" />
                <input type="text" class="product-sn" index="<?=$i?>" name="GoodsForm[products][<?=$i?>][sn]" size="15" value="<?=$cur->sn?>" />
            </td>
            <td><input type="text" name="GoodsForm[products][<?=$i?>][sn_intel]" size="15" value="<?=$cur->sn_intel?>" /></td>
            <td><input type="text" name="GoodsForm[products][<?=$i?>][sn_purchase]" size="15" value="<?=$cur->sn_purchase?>" /></td>
            <td><input type="text" name="GoodsForm[products][<?=$i?>][name_short]" value="<?=$cur->name_short?>" /></td>
            <td><input type="text" name="GoodsForm[products][<?=$i?>][price_cost]" value="<?=$cur->price_cost?>" size="7" class="money product-price-cost" /></td>
            <td><input type="text" name="GoodsForm[products][<?=$i?>][price_market]" value="<?=$cur->price_market?>" size="7" class="money" /></td>
            <td><input type="text" name="GoodsForm[products][<?=$i?>][slot]" value="<?=$cur->slot?>" size="7" /></td>
            <td><input type="text" name="GoodsForm[products][<?=$i?>][weight]" value="<?=$cur->weight?>" size="7" class="money product-weight" /></td>
            <td class="price-step-list">
                <input type="hidden" name="GoodsForm[products][<?=$i?>][type][1][id]" value="<?=$mins[1]->id?>" />
                <input type="text" size="7" name="GoodsForm[products][<?=$i?>][type][1][step]" class="number" placeholder="递增量" title="递增量" value="<?=$mins[1]->step?>" /> 
                <a href="javascript:void(0);" onclick="listPriceStep(this);" title="显示所有价格梯度">+</a>
                <div class="price-step">
                    <input type="hidden" name="GoodsForm[products][<?=$i?>][type][1][prices][0][id]" value="<?=$prices[1][0]->id?>" />
                    <input type="text" name="GoodsForm[products][<?=$i?>][type][1][prices][0][min]" value="<?=$prices[1][0]->number?>" class="number" size="7" placeholder="起订量" title="起订量" />
                    <input type="text" name="GoodsForm[products][<?=$i?>][type][1][prices][0][price]" value="<?=$prices[1][0]->price?>" class="money" size="7" placeholder="价格" title="价格" />
                </div>
                <div class="price-step none">
                    <input type="hidden" name="GoodsForm[products][<?=$i?>][type][1][prices][1][id]" value="<?=$prices[1][1]->id?>" />
                    <input type="text" name="GoodsForm[products][<?=$i?>][type][1][prices][1][min]" value="<?=$prices[1][1]->number?>" class="number" size="7" placeholder="起订量" title="起订量" />
                    <input type="text" name="GoodsForm[products][<?=$i?>][type][1][prices][1][price]" value="<?=$prices[1][1]->price?>" class="money" size="7" placeholder="价格" title="价格" />
                </div>
                <div class="price-step none">
                    <input type="hidden" name="GoodsForm[products][<?=$i?>][type][1][prices][2][id]" value="<?=$prices[1][2]->id?>" />
                    <input type="text" name="GoodsForm[products][<?=$i?>][type][1][prices][2][min]" value="<?=$prices[1][2]->number?>" class="number" size="7" placeholder="起订量" title="起订量" />
                    <input type="text" name="GoodsForm[products][<?=$i?>][type][1][prices][2][price]" value="<?=$prices[1][2]->price?>" class="money" size="7" placeholder="价格" title="价格" />
                </div>
            </td>
            <td class="price-step-list">
                <input type="hidden" name="GoodsForm[products][<?=$i?>][type][2][id]" value="<?=$mins[2]->id?>" />
                <input type="text" size="7" name="GoodsForm[products][<?=$i?>][type][2][step]" value="<?=$mins[2]->step?>" class="number" placeholder="递增量" title="递增量" /> 
                <a href="javascript:void(0);" onclick="listPriceStep(this);" title="显示所有价格梯度">+</a>
                <div class="price-step">
                    <input type="hidden" name="GoodsForm[products][<?=$i?>][type][2][prices][0][id]"  value="<?=$prices[2][0]->id?>" />
                    <input type="text" name="GoodsForm[products][<?=$i?>][type][2][prices][0][min]" value="<?=$prices[2][0]->number?>" class="number" size="7" placeholder="起订量" title="起订量" />
                    <input type="text" name="GoodsForm[products][<?=$i?>][type][2][prices][0][price]" value="<?=$prices[2][0]->price?>" class="money" size="7" placeholder="价格" title="价格" />
                </div>
                <div class="price-step none">
                    <input type="hidden" name="GoodsForm[products][<?=$i?>][type][2][prices][1][id]" value="<?=$prices[2][1]->id?>" />
                    <input type="text" name="GoodsForm[products][<?=$i?>][type][2][prices][1][min]" value="<?=$prices[2][1]->number?>" class="number" size="7" placeholder="起订量" title="起订量" />
                    <input type="text" name="GoodsForm[products][<?=$i?>][type][2][prices][1][price]" value="<?=$prices[2][1]->price?>" class="money" size="7" placeholder="价格" title="价格" />
                </div>
                <div class="price-step none">
                    <input type="hidden" name="GoodsForm[products][<?=$i?>][type][2][prices][2][id]" value="<?=$prices[2][2]->id?>" />
                    <input type="text" name="GoodsForm[products][<?=$i?>][type][2][prices][2][min]" value="<?=$prices[2][2]->number?>" class="number" size="7" placeholder="起订量" title="起订量" />
                    <input type="text" name="GoodsForm[products][<?=$i?>][type][2][prices][2][price]" value="<?=$prices[2][2]->price?>" class="money" size="7" placeholder="价格" title="价格" />
                </div>
            </td>
            <td class="price-step-list">
                <input type="hidden" name="GoodsForm[products][<?=$i?>][type][3][id]" value="<?=$mins[3]->id?>" />
                <input type="text" name="GoodsForm[products][<?=$i?>][type][3][step]" value="<?=$mins[3]->step?>" size="7" class="number" placeholder="递增量" title="递增量" /> 
                <a href="javascript:void(0);" onclick="listPriceStep(this);" title="显示所有价格梯度">+</a>
                <div class="price-step">
                    <input type="hidden" name="GoodsForm[products][<?=$i?>][type][3][prices][0][id]"  value="<?=$prices[3][0]->id?>" />
                    <input type="text" name="GoodsForm[products][<?=$i?>][type][3][prices][0][min]" value="<?=$prices[3][0]->number?>" class="number" size="7" placeholder="起订量" title="起订量" />
                    <input type="text" name="GoodsForm[products][<?=$i?>][type][3][prices][0][price]" value="<?=$prices[3][0]->price?>" class="money" size="7" placeholder="价格" title="价格" />
                </div>
                <div class="price-step none">
                    <input type="hidden" name="GoodsForm[products][<?=$i?>][type][3][prices][1][id]" value="<?=$prices[3][1]->id?>" />
                    <input type="text" name="GoodsForm[products][<?=$i?>][type][3][prices][1][min]" value="<?=$prices[3][1]->number?>" class="number" size="7" placeholder="起订量" title="起订量" />
                    <input type="text" name="GoodsForm[products][<?=$i?>][type][3][prices][1][price]" value="<?=$prices[3][1]->price?>" class="money" size="7" placeholder="价格" title="价格" />
                </div>
                <div class="price-step none">
                    <input type="hidden" name="GoodsForm[products][<?=$i?>][type][3][prices][2][id]"  value="<?=$prices[3][2]->id?>" />
                    <input type="text" name="GoodsForm[products][<?=$i?>][type][3][prices][2][min]" value="<?=$prices[3][2]->number?>" class="number" size="7" placeholder="起订量" title="起订量" />
                    <input type="text" name="GoodsForm[products][<?=$i?>][type][3][prices][2][price]" value="<?=$prices[3][2]->price?>" class="money" size="7" placeholder="价格" title="价格" />
                </div>
            </td>
        </tr>
        <?php 
        $i++;
        endforeach;?>
    </table>
</div>


<?php
function combos($data, &$all = array(), $group = array(), $val = null, $i = 0) {
    if (isset($val)) {
        array_push($group, $val);
    }

    if ($i >= count($data)) {
        array_push($all, $group);
    } else {
        foreach ($data[$i] as $v) {
            combos($data, $all, $group, $v, $i + 1);
        }
    }

    return $all;
}

?>


<script>
    function sku_init(){
        $('.sku-checkbox').click(function() {
            var attr_id = $(this).attr('attr_id');
            var index = $(this).attr('index');
            var is_aliases=$(this).attr('is_aliases');
            var is_upload_img=$(this).attr('is_upload_img');
            var customId = attr_id + '_' + index;
            var isChecked = $(this).is(":checked");

            if (is_aliases == 1) {
                if (isChecked) {
                    $('#sku_name_'+customId).css('display', 'none');
                    $('#sku_name_custom_'+customId).css('display', 'inline-block');
                } else {
                    $('#sku_name_'+customId).css('display', 'inline-block');
                    $('#sku_name_custom_'+customId).css('display', 'none');
                }
            }
            skuImgList(attr_id,index,isChecked);
            skuMapRender();
        });
        $('.sku-custom-name').change(function() {
            var newName=$(this).val();
            var attr_id = $(this).attr('attr_id');
            var index = $(this).attr('index');
            $('.sku-item-name[sku_name_id='+attr_id+'_'+index+']').html(newName);
            skuMapRender();
        });
    }
    
    
    function uploadAttrImg(id){
        if ($('#attr_img_form').find('#upload_div_' + id).length == 0) {
            $('#attr_img_form').append('\
                <div id="upload_div_' + id + '">\n\
                <form id="upload_form_' + id + '" enctype="multipart/form-data" method="POST" action="<?=Url::to(['/upload/attr-img'])?>" target="upload_iframe_' + id + '">\n\
                <input type="hidden" name="_csrf" value="<?=Yii::$app->request->csrfToken?>" >\n\
                <input type="hidden" name="id" value="' + id + '" >\n\
                <input type="file" name="img" id="upload_input_' + id + '" onchange="uploadAttrImg_change(this)" >\n\
                </form>\n\
                <iframe id="upload_iframe_' + id + '" name="upload_iframe_' + id + '"></iframe>\n\
                </div>\n\
            ');
        }
        $('#upload_input_' + id).click();
    }
    
    function uploadAttrImg_change(e) {
        var id = $(e).attr('id');
        var formId = id.replace('upload_input_', 'upload_form_');
        $('#' + formId).submit();
    }
    
    function setAttrImgUrl(id, imgUrl,path){
        $('#attr_img_' + id).val(path);
        $('#attr_img_src_' + id).attr('src', imgUrl);
        $('#upload_input_' + id).val('');
    }
    
    function skuImgList(attr_id,index,isList){
        index=parseInt(index)+1;
        var e=$('.upload-img-list[attr_id='+attr_id+'] tr:eq('+index+')');
        if(isList){
            e.removeClass('none');
        }else{
            e.addClass('none');
        }
    }
    
    function skuMapRender(){
        var isAllSelected=true;
        var selectedVals = [];

        //提取数据
        $('.sku-item ul').each(function(){
            if(isAllSelected===false){
                return ;
            }
            var t = [];
            $(this).find('.sku-checkbox:checked').each(function(){
                var attr_id=$(this).attr('attr_id');
                var index=$(this).attr('index');
                t.push(attr_id+'__'+index);
            });
            
            if (t.length > 0){
                selectedVals.unshift(t);
            }else{
                isAllSelected=false;
            }
        });
        
        product_render(selectedVals);
    }
    
    function product_render(specVal){
        function combine(arr) {
            var r = [];
            (function f(t, a, n) {
                if (n == 0)
                    return r.push(t);
                for (var i = 0; i < a[n - 1].length; i++) {
                    f(t.concat(a[n - 1][i]), a, n - 1);
                }
            })([], arr, arr.length);
            return r;
        }

        var res = combine(specVal);
        
        $('.product-map-item').addClass('none');
        for(var i=0;i<res.length;i++){
            var id='#pro_item_'+res[i].join('_');
            $(id).removeClass('none');
        }
    }
    
    function product_init(){
        sku_init();
        format_init();
        product_sn_change();
    }
    
    
</script>
